import { defineEventHandler, EventHandlerRequest } from 'h3'; import { DB } from '~~/server/db/DB'; import { createErrorResponse, createSuccessResponse, IResponse } from '~~/server/utils/response'; import { IChannel } from '../channel/[id]'; export interface IArticle { id: number; model_id: number; channel_id: number; title: string; desc: string; image: string, video?: string, images: string; seotitle: string; keywords: string; description: string; tags: string; diyname: string; publishtime: number; createtime: number; views: number; content: string; channel: IChannel; outlink?: string; } export default defineEventHandler>>(async (event) => { try { const id = event.context.params?.id; if (!id) return createErrorResponse('分类ID不能为空'); const article = await DB.table('pr_cms_archives') .where('id', id) .whereNull('deletetime') .where('status', 'normal') .first(); if (!article) return createErrorResponse('文章不存在'); const channel = await DB.table('pr_cms_channel') .where('id', article.channel_id) .first(); if (!channel) return createErrorResponse('分类不存在'); article.channel = channel; // 2. 通过model_id从pr_cms_model表中获取table字段 const model = await DB.table('pr_cms_model') .where('id', article.model_id) .select('table') .first(); if (!model) return createErrorResponse('分类不存在'); // 3. 通过table指定的表通过id查出content const content = await DB.table(`pr_${model.table}`) .where('id', id) .select('content') .first(); // 4. 合并返回结果 if (content && content['content']) { article.content = content['content']; } return createSuccessResponse(article); } catch (error) { return createErrorResponse(error); } });